/**
 * 
 */
package pt.mleiria.sAction.dao.index;

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pt.mleiria.sAction.hibernate.Index;
import pt.mleiria.sAction.hibernate.IndexData;

/**
 * @author Manuel
 *
 */
public class IndexDAOImpl extends HibernateDaoSupport implements IndexDAO {

	/* (non-Javadoc)
	 * @see pt.mleiria.sAction.dao.index.IndexDAO#getIndexList(java.lang.String, int)
	 */
	public List<IndexData> getIndexDataList(String ticker, int numberOfSessions) throws DataAccessException{
		Index index = getIndex(ticker);
		List<IndexData> results = getHibernateTemplate().find(" from IndexData indexData where indexData.index = ? order" +
				" by indexData.stockDate desc", index);
		List<IndexData> indexDataList = new ArrayList<IndexData>();
		if (numberOfSessions == 0){
			return results;
		}else{
			for(int i = 0, n = results.size(); i < n; i++){
				if(i == 51)
					return indexDataList;
				else
					indexDataList.add(results.get(i));
			
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see pt.mleiria.sAction.dao.index.IndexDAO#getIndex(java.lang.String)
	 */
	public Index getIndex(String ticker) {
		List<Index> results = getHibernateTemplate().find("from Index index where index.ticker = '" + ticker + "'"); 
		return results.size() > 0 ? results.get(0) : null;
	}
	
	


}
